﻿;$(document).ready(function () {
    var redirectUrl = $axshare.getQueryVariable("redirect");

    $axshare.auth(function(response) {
        if(response.success) {
            $axshare.setAuthCookie(response.authToken);
            if (redirectUrl) {
                window.location.href = decodeURIComponent(redirectUrl);
            } else {
                window.location.href = $axshare.urlPaths.fsmanage;
            }
        }
    });

    var clearErrorMessages = function () {
        $axure('@LoginMessage').text('');
        $axure('@CreateMessage').text('');
        $axure('@PrototypeMessage').text('');
    };

    clearErrorMessages();

    $axure('@LoginMessagePanel').show();
    $axure('@CreateMessagePanel').show();
    $axure('@PrototypeMessagePanel').show();

    var loginFunc = function () {
        clearErrorMessages();

        $axure('@LoginProgress').show();

        var password = $axure('@Password').value();
        var staySignedIn = $axure('@StaySignedIn').value();
        var loginEmail = $.trim($axure('@Email').value());

        $axshare.login(loginEmail, password, staySignedIn,
            function (response) {
                $axure('@LoginProgress').hide();
                if (response.success == true) {
                    $axshare.setAuthCookie(response.authToken);
                    var hash = window.location.hash;
                    var r = response.redirecturl || (
                        redirectUrl
                            ? decodeURIComponent(redirectUrl)
                            : $axshare.urlPaths.fsmanage);

                    window.location.href = r + hash;
                } else {
                    if (response.message) $axure('@LoginMessage').text(response.message);
                    $axure('@Password').$().find('input').val(''); //eventually change when call is fixed
                    $axure('@Password').focus();
                }
            }
        );
    };

    var loginButton = $axure('@Login');
    if (loginButton) loginButton.click(loginFunc).css('cursor', 'pointer');

    //Attach onEnter events to loginfunc
    var loginEmailField = $axure('@Email');
    var loginPassField = $axure('@Password');

    loginEmailField.$().keydown(function (event) {
        if (event.which == '13') {
            loginFunc();
        }
    });
    loginPassField.$().keydown(function (event) {
        if (event.which == '13') {
            loginFunc();
        }
    });


    var createFunc = function () {
        clearErrorMessages();

        var userEmail = $.trim($axure('@CreateEmail').value());
        var password = $axure('@CreatePassword').value();

        var emailRegex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        var isValidEmail = emailRegex.test(userEmail);

        if (!isValidEmail) {
            $axure('@CreateMessage').text('Email address is not valid.');

            $axure('@CreateEmail').focus();

            return;
        }

        if (password.length < 8) {
            $axure('@CreateMessage').text('Your password must contain at least 8 characters.');

            $axure('@CreatePassword').focus();

            return;
        }

        var agreedToTerms = $axure('@agreeTermsCheckbox').value();

        if (!agreedToTerms) {
            $axure('@CreateMessage').text('Please agree to the Axure terms to proceed.');

            return;
        }

        $axure('@CreateAccountProgress').show();

        $axshare.createAccount(userEmail, password, agreedToTerms, 1,
            function (response) {
                $axure('@CreateAccountProgress').hide();

                if(response.success == true) {
                    $axshare.setAuthCookie(response.authToken);
                    window.location.href = $axshare.urlPaths.fsmanage;
                } else {
                    if (response.message)
                        $axure('@CreateMessage').text(response.message);
                }

            },
            function(errorMessage) {
                $axure('@CreateAccountProgress').hide();
                $axure('@CreateMessage').text(errorMessage);
            }
        );
    };

    var createButton = $axure('@CreateAccount');
    if (createButton) createButton.click(createFunc).css('cursor', 'pointer');

    //Attach onEnter events to loginfunc
    var createEmailField = $axure('@CreateEmail');
    var createPassField = $axure('@CreatePassword');

    createEmailField.$().keydown(function (event) {
        if (event.which == '13') {
            createFunc();
        }
    });
    createPassField.$().keydown(function (event) {
        if (event.which == '13') {
            createFunc();
        }
    });

    var passwordButton = $axure('@ForgotPassword');
    if (passwordButton) passwordButton.click(function () {
        clearErrorMessages();

        var useremail = $.trim($axure('@Email').value());
        if (!useremail || useremail == 'Email') {
            $axure('@LoginMessage').text('Please enter an email address.');
        } else {
            $axshare.forgotPassword(useremail, 1,
                function(response) {
                    if(response.message) {
                        if (response.message) $axure('@LoginMessage').text(response.message);
                    }
                }
            );
        }
    }).css('cursor', 'pointer');

    var prototypeGoFunc = function () {
        clearErrorMessages();

        var prototypeId = $.trim($axure('@PrototypeId').value());
        if (!prototypeId || prototypeId.length != 6) {
            $axure('@PrototypeMessage').text('Please enter a valid prototype ID.');
        } else {
            window.location.href = '/' + prototypeId;
        }
    };

    var prototypeButton = $axure('@PrototypeGo');
    if (prototypeButton) prototypeButton.click(prototypeGoFunc).css('cursor', 'pointer');

    //Attach onEnter events to loginfunc
    var prototypeIdField = $axure('@PrototypeId');

    prototypeIdField.$().keydown(function (event) {
        if (event.which == '13') {
            prototypeGoFunc();
        }
    });
});
